"""
JSON 解码器
"""

import decimal
import json
import uuid

from pydantic import BaseModel

from config import settings
from my_typing import *


class JSONEncoder(json.JSONEncoder):
    """
    JSON 解码器
    """

    def default(self, obj):
        if isinstance(obj, date):
            return obj.strftime(settings.FORMAT.DATE_FORMAT)

        elif isinstance(obj, time):
            return obj.strftime(settings.FORMAT.TIME_FORMAT)

        elif isinstance(obj, datetime):
            return obj.strftime(settings.FORMAT.DATETIME_FORMAT)

        elif isinstance(obj, timedelta):
            return str(obj.total_seconds())

        elif isinstance(obj, decimal.Decimal):
            return float(obj)

        elif isinstance(obj, uuid.UUID):
            return str(obj)

        elif isinstance(obj, bytes):
            return obj.decode()

        elif isinstance(obj, BaseModel):
            return obj.model_dump()

        elif hasattr(obj, "__iter__"):
            return tuple(item for item in obj)

        return super().default(obj)
